package com.zts.modules.sys.mapper;

/**
 * Created by jisd on 2017/6/16.
 */

import com.google.common.base.Strings;
import com.zts.modules.sys.entity.Resource;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public class ResourceSqlProvider {

    public String get(Long id) {
        return (new SQL() {
            {
                SELECT("r.*");
                FROM(" sys_resource r  ");
                WHERE(" r.id = #{id} ");
            }
        }).toString();
    }

    public String getByEntity(final Resource resource) {
        return (new SQL() {
            {
                SELECT("r.*");
                FROM(" sys_resource r  ");
                if (!Strings.isNullOrEmpty(resource.getResourceName())) {
                    WHERE("r.resource_name = #{resourceName}");
                }

                if (null != resource.getParentId() && resource.getParentId().longValue() > 0) {
                    WHERE("r.parentid = #{parentId}");
                }

                if (!Strings.isNullOrEmpty(resource.getType())) {
                    WHERE("r.type = #{type}");
                }

                if (!Strings.isNullOrEmpty(resource.getResourceCode())) {
                    WHERE("r.resource_code = #{resourceCode}");
                }

                if (null != resource.getId() && resource.getId().longValue() > 0) {
                    WHERE("r.id  = #{id}");
                }

                ORDER_BY(" r.id ");
            }
        }).toString();
    }

    public String getList(Resource resource) {
        return getByEntity(resource);
    }

    public String update(final Resource resource) {
        return (new SQL() {
            {
                UPDATE("sys_resource");
                if (!Strings.isNullOrEmpty(resource.getResourceName())) {
                    SET("resource_name = #{resourceName}");
                }

                if (!Strings.isNullOrEmpty(resource.getStatus())) {
                    SET("status = #{status}");
                }

                if (null != resource.getParentId() && resource.getParentId().longValue() > 0L) {
                    SET("parentid = #{parentId}");
                }

                if (!Strings.isNullOrEmpty(resource.getType())) {
                    SET("type = #{type}");
                }

                if (!Strings.isNullOrEmpty(resource.getResourceCode())) {
                    SET("resource_code = #{resourceCode}");
                }

                if (!Strings.isNullOrEmpty(resource.getResourceValue())) {
                    SET("resource_value = #{resourceValue}");
                }

                WHERE("id = #{id}");
            }
        }).toString();
    }

    public String insert(final Resource resource) {
        return (new SQL() {
            {
                INSERT_INTO("sys_resource");
                VALUES("resource_name", "#{resourceName}");
                VALUES("parentid", "#{parentId}");
                if (!Strings.isNullOrEmpty(resource.getStatus())) {
                    VALUES("status", "#{status}");
                } else {
                    VALUES("status", "1");
                }

                if (!Strings.isNullOrEmpty(resource.getType())) {
                    VALUES("type", "#{type}");
                } else {
                    VALUES("type", "1");
                }

                VALUES("resource_value", "#{resourceValue}");
                VALUES("sort_num", "#{sortNum}");
                VALUES("resource_code", "#{resourceCode}");
                VALUES("create_time", "now()");
            }
        }).toString();
    }

    public String delete(final Resource resource) {
        return (new SQL() {
            {
                UPDATE("sys_resource");
                SET("status ='0' ");
                WHERE("id = #{id}");
            }
        }).toString();
    }


    public String getByRoles(final Map<String, Object> param) {
        return (new SQL() {
            {
                SELECT("distinct a.*");
                FROM(" sys_resource a ,sys_role_resource b  ");
                WHERE("  a.id = b.resource_id and a.status = '1' ");
                if (!Strings.isNullOrEmpty((String) param.get("roleIds"))) {
                    WHERE("  b.role_id in (" + param.get("roleIds") + ")");
                }
                if (!Strings.isNullOrEmpty((String) param.get("type"))) {
                    WHERE("  a.type = #{type}");
                }
                if (!Strings.isNullOrEmpty((String) param.get("resourceCode"))) {
                    WHERE("  a.resource_code = #{resourceCode}");
                }
            }
        }).toString();
    }

    /**
     * 给多个资源分配角色
     *
     * @param param
     * @return
     */
    public String addRoleResource(final Map<String, Object> param) {
        //values语句
        StringBuffer sql = new StringBuffer("");
        //执行sql
        String sqlStr = "";
        sql.append("insert into sys_role_resource (role_id,resource_id) values ");
        //遍历组装values语句
        Long roleId = (Long) param.get("roleId");
        //String rIds = (String) param.get("resourceIds");
        List<Long> resourceIds = (List<Long>) param.get("resourceIds");
        if (roleId > 0 && null != resourceIds) {
            for (Long s : resourceIds) {
                sql.append(" ( ").append(roleId).append(",").append(s).append(" )");//role_id
                sql.append(",");
            }
            sqlStr = sql.toString();
            sqlStr = sqlStr.substring(0, sqlStr.length() - 1); //删除最后一个,
        }
        return sqlStr;
    }

    /**
     * 给多个资源分配角色
     *
     * @param param
     * @return
     */
    public String deleteRoleResource(final Map<String, Object> param) {
        //values语句
        StringBuffer sql = new StringBuffer("");
        //执行sql
        String sqlStr = "";
        sql.append(" delete from sys_role_resource a  ");
        //遍历组装values语句
        Long roleId = (Long) param.get("roleId");
        List<Long> resourceIds = (List<Long>) param.get("resourceIds");
        if (roleId > 0 && null != resourceIds) {
            sql.append(" where  ");
            for (Long s : resourceIds) {
                sql.append(" ( a.role_id = ").append(roleId).append(" and a.resource_id = ").append(s).append(" ) ");//role_id
                sql.append(" ||");
            }
            sqlStr = sql.toString();
            sqlStr = sqlStr.substring(0, sqlStr.length() - 2); //删除最后一个||
        } else {
            sql.append(" where  1 = 2  ");
            sqlStr = sql.toString();
        }

        return sqlStr;
    }
}